<extend name='Block/Admin_code'/>
<block name="content" >
<div class="panel panel-success xtl" style='width:310px;'>
	 <div class='panel-heading'>数据库--{:C('DB_NAME')}({:count($tb)})</div>
	 <div class='panel-body'>
	 	<table class=" table table-striped" border='1' bordercolor="#dedede">
	 	   <tr><td>数据表</td></tr>
	 	   <volist name='tb' id="tb">
		    <tr>
		       <td>
		           {$tb}
		           <span class='btn btn-info  fr tbname' data-id='{$tb}'>详情</span>
		        </td>	
		    </tr>
		   </volist> 
		</table>
	 </div>
</div>
<div class="panel panel-info xtr">
	 <div class='panel-heading'>数据表详情</div>
	 <div class='panel-body'>
	 	 <table class=" table table-striped" border='1' bordercolor="#dedede">
		 	   <tr><td colspan="5">数据表：<span id='tbname'></span></td></tr>
		 	   <tr><td colspan="5">字段前缀：<span id='prefix'></span></td></tr>
		 	   <tr >
		   	    	<td>字段</td>
		   	    	<td>注释</td>
		   	    	<td>类别</td>
		   	    	<td>是否主键</td>
		   	    	<td>默认值</td>
		   	    </tr>
		   	    <tbody id="fieldList">
		   	    	
		   	    </tbody>
		   	    <tr>
		   	        <td colspan='5'>
		   	            <input type='hidden' name='fields'  value="" />
		   	        	<input type="submit" class="btn btn-primary buildModel" value="生成Model" tablename="" data-toggle="modal" data-target="#myModal" data-type='model'>

		   	        	<input type="submit" class="btn btn-primary buildViewModel" value="生成ViewModel" tablename="" data-toggle="modal" data-target="#myModal" data-type='viewmodel'>

		   	        	<input type="submit" class="btn btn-primary buildController" value="生成Controller" tablename="" data-toggle="modal" data-target="#myModal" data-type='controller'>

		   	        	<input type="submit" class="btn btn-primary buildView" value="生成View" tablename="" data-toggle="modal" data-target="#myModal" data-type='view'>

		   	        	<input type="submit" class="btn btn-primary buildAll" value="全部生成" tablename="" data-toggle="modal" data-target="#myModal" data-type='all'>
		   	        </td>
		   	    </tr>
		 </table>
	 </div>
</div>
<script>
        $(document).ready(function(){
        	var tbname=$('.tbname').eq(0).data('id');
             post(tbname)
        	 $(".tbname").click(function(){
        	 	 var tbname=$(this).data('id');
        	 	 post(tbname);
        	 });
        	 $("input[type='submit']").click(function(){
        	 	$("#xkcodeview").html('正在加载中...');
        	 	var type=$(this).data('type');
        	 	var tbName=$(this).attr('tablename');
                $("#makecode").attr('data-name',tbName);
                $("#makecode").attr('data-type',type);
        	 	getCode(tbName,type,false);
        	 });
        	 $("#makecode").click(function(){
        	 	var type=$(this).attr('data-type');
        	 	var tbName=$(this).attr('data-name');
        	 	getCode(tbName,type,'is');
        	 });
        });
        function post(tbname){
        	var url="{:U('getfields')}";
        	$.post(url,{tbname:tbname},function(data){
        	 	 	//console.log(data);
        	 	 	data=eval("("+data+")");
        	 	 	field=data.field;
        	 	 	var tr='';
        	 	 	for (var p in field){
        	 	 		tr+='<tr><td>'+field[p]['field']+'</td><td>'+field[p]['comment']+'</td><td>'+field[p]['type']+'</td><td>'+field[p]['key']+'</td><td>'+field[p]['default']+'</td></tr>'
        	 	 	}
        	 	 	//表名
        	 	 	$("#tbname").text(data.tbName);
        	 	 	$("input[type='submit']").attr('tablename',data.tbName)
        	 	 	$("input[name='fields']").val(data.codefield);
        	 	 	$("#prefix").text(data.prefix);
        	 	 	$("#fieldList").html(tr);
        	 	 });
        }
        function getCode(name,type,ismake){
        	var url="{:U('getCode')}";
        	var fields=$("input[name='fields']").val();
        	$.post(url,{name:name,type:type,fields:fields,ismake:ismake},function(data){
                if(ismake=='is'){
                	alert(data);
                }else{
                	$("#xkcodeview").html(data);
                }
                
        	});
        }
   </script>
   <!-- 模态框（Modal） -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" 
	   aria-labelledby="myModalLabel" aria-hidden="true">
	   <div class="modal-dialog">
	      <div class="modal-content">
	         <div class="modal-header">
	            <button type="button" class="close" 
	               data-dismiss="modal" aria-hidden="true">
	                  &times;
	            </button>
	            <h4 class="modal-title" id="myModalLabel">
	               代码预览
	            </h4>
	         </div>
	         <div class="modal-body">
	             <pre id='xkcodeview'>
	             	
	             </pre>
	         </div>
	         <div class="modal-footer">
	            <button type="button" class="btn btn-default" 
	               data-dismiss="modal">关闭
	            </button>
	            <button type="button" class="btn btn-primary" id='makecode' data-name='' data-type=''>
	               提交生成
	            </button>
	         </div>
	      </div><!-- /.modal-content -->
	</div><!-- /.modal -->
</block>